package com.cskaoyan.demo4.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;

public class MybatisUtils {


    public static SqlSession getSqlSession(){

        // // 1. 获取一个SqlSessionFactoryBuilder
        // SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        //
        // // 2. 获取一个SqlSessionFactory
        // InputStream inputStream = null;
        // try {
        //     inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        // } catch (IOException e) {
        //     throw new RuntimeException(e);
        // }
        // SqlSessionFactory sqlSessionFactory = builder.build(inputStream);
        //
        // // 3. 获取SqlSession(就相当于是连接对象)
        // // SqlSession sqlSession = sqlSessionFactory.openSession();
        // SqlSession sqlSession = sqlSessionFactory.openSession(true);

        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        InputStream in = MybatisUtils.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory factory = builder.build(in);
        return factory.openSession(true);

    }
}
